package gpx;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;

import javax.swing.*;

/**
 * 
 * @author Josef Vermach jr.
 *
 */
public class FileViewer extends JFrame{
	TextArea textSpace;
	File file;
	String language;
	Boolean isChanged = false;
	
	/**
	 * Konstruktor creates new instance of Object FileViewer - Frame with content of file is displayed. 
	 * @param f - file which is displayed
	 * @param language - name of properties file with name of language
	 */
	public FileViewer (final File f, final String language){
		this.file = f;
		this.language = language;
		this.setLayout(new BorderLayout(10,10));
		textSpace = new TextArea();
		textSpace.setEditable(false);
		this.add(textSpace, "Center");
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent arg0) {
				System.out.println("LOG: opening");
				//TODO Log
				
			}
			
			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.out.println("closing");
				//TODO Log
				if (isChanged){
					System.out.println("ischanged");
//					JOptionPane.showMessageDialog(rootPane, Localization.getString(language, "MESSAGES.NoFile"));
					Object[] options = {"Yes","No"};
					int n = JOptionPane.showOptionDialog(rootPane, 
							Localization.getString(language,"FILEVIEWER.SaveChanges"),
							Localization.getString(language,"FILEVIEWER.CHANGES"),
							JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE,
							null, 
							options,
							options[0]);
					if (n == JOptionPane.YES_OPTION) {
						save();
					}
					else if (n == JOptionPane.NO_OPTION) {
						
					}
					else {
						System.out.println("nothing");
					}
					
				}
				
			}
			
			@Override
			public void windowClosed(WindowEvent arg0) {
				System.out.println("closet");
				//TODO Log
				
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		MenuBar mb = new MenuBar(); //Creating of menu.
		this.setMenuBar(mb);
		Menu mEdit = new Menu(Localization.getString(language, "EDIT"));
		mb.add(mEdit);
		final CheckboxMenuItem mIEdit = new CheckboxMenuItem(Localization.getString(language, "EDIT"),false);
		mEdit.add(mIEdit);
		mIEdit.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (!mIEdit.getState()) {
					textSpace.setEditable(false);
				}
				if (mIEdit.getState()) {
					textSpace.setEditable(true);
				}
			}
		});
		textSpace.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				if (textSpace.isEditable()){
					isChanged=true;
				}
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
			}
			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
//		this.textSpace.append("1juuhu\n");
//		this.textSpace.append("2juuhu\n");
//		this.textSpace.append("3uuhu\n");
//		this.textSpace.append("4uuhu\n");
//		this.textSpace.append("5uuhu\n");
//		this.textSpace.append("6uuhu\n");
//		this.textSpace.append("7uuhu\n");
//		this.textSpace.append("8uuhu\n");
//		this.textSpace.append("9uuhu\n");
//		this.textSpace.append("10uhu\n");
		System.out.println("ass");
		this.pack();
		this.changePosition();
		this.setTitle(Localization.getString(language, "FileViewerTitle"));
		this.setVisible(true);
	}

	/**
	 * Method sets position of FileViewer JFrame
	 */
	private void changePosition() {
		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(((screenDim.width-getSize().height) / 3),((screenDim.height - getSize().height)/3));
	}

	/*
	 * Method closes whole program.
	 */
	public void close() {
		System.out.println("Close pressed");
		dispose();
		System.out.println("Program will now exit.");
		System.exit(0);
	}
	

	
	public void setText(String text) {
		textSpace.setText(text);
	}
	
	/**
	 * Method views a contents of a text files.
	 * @param file
	 */
	public void view () {
		try {
	      String text = "";
	      DataInputStream in = new DataInputStream(new FileInputStream(this.file));
	      while (in.available()>0) {
	        text += in.readLine()+"\n";
	        }
	      this.setText(text);
	      }
	      catch (FileNotFoundException e) {
	          System.out.println("FileNotFoundException: File was not found.");
	        }
	      catch (IOException e) {
	        System.out.println("IOEXception: Error due reading file.");
	        }
	}
	
	public void save(){
		System.out.println("Save button was pressed");
		Frame frame = new Frame();
		String filename = File.separator+"tmp";
		JFileChooser fc = new JFileChooser(new File(filename));
		File selFile = fc.getSelectedFile();
		
		// Show save dialog; this method does not return until the dialog is closed
		fc.showSaveDialog(frame);
		selFile = fc.getSelectedFile();
	}
	
	public void saveFile() {
			//String line = null;;
			File file = this.file;
			File newFile = new File("newfile.txt");
			String line = "kocka lezde do";
			try {
				System.out.println(textSpace.getText().length());
				BufferedReader in = new BufferedReader (new FileReader(file));
				BufferedWriter out = new BufferedWriter(new FileWriter(newFile));
				while (line!= null) {
					out.write(line);
					line=in.readLine();
				}		
				in.close();
				out.close();
			
//				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			return line;
//		}
//
//		/**
//		 * Method saves current language settings into language.txt file.
//		 */
//		public void saveLanguage() {
//			File file = new File ("language.txt");
//			try {
//				BufferedWriter out = new BufferedWriter(new FileWriter(file));
//				out.write(this.language);
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
	}

